package cn.kkdlk.generate.dxf.model.base;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Color {
    /**
     * The color white.  In the default sRGB space.
     */
    public final static Color white = new Color(255, 255, 255);

    /**
     * The color white.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color WHITE = white;


    /**
     * The color light gray.  In the default sRGB space.
     */
    public final static Color lightGray = new Color(192, 192, 192);

    /**
     * The color light gray.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color LIGHT_GRAY = lightGray;

    /**
     * The color gray.  In the default sRGB space.
     */
    public final static Color gray = new Color(128, 128, 128);

    /**
     * The color gray.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color GRAY = gray;

    /**
     * The color dark gray.  In the default sRGB space.
     */
    public final static Color darkGray = new Color(64, 64, 64);

    /**
     * The color dark gray.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color DARK_GRAY = darkGray;

    /**
     * The color black.  In the default sRGB space.
     */
    public final static Color black = new Color(0, 0, 0);

    /**
     * The color black.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color BLACK = black;

    /**
     * The color red.  In the default sRGB space.
     */
    public final static Color red = new Color(255, 0, 0);

    /**
     * The color red.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color RED = red;

    /**
     * The color pink.  In the default sRGB space.
     */
    public final static Color pink = new Color(255, 175, 175);

    /**
     * The color pink.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color PINK = pink;

    /**
     * The color orange.  In the default sRGB space.
     */
    public final static Color orange = new Color(255, 200, 0);

    /**
     * The color orange.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color ORANGE = orange;

    /**
     * The color yellow.  In the default sRGB space.
     */
    public final static Color yellow = new Color(255, 255, 0);

    /**
     * The color yellow.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color YELLOW = yellow;

    /**
     * The color green.  In the default sRGB space.
     */
    public final static Color green = new Color(0, 255, 0);

    /**
     * The color green.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color GREEN = green;

    /**
     * The color magenta.  In the default sRGB space.
     */
    public final static Color magenta = new Color(255, 0, 255);

    /**
     * The color magenta.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color MAGENTA = magenta;

    /**
     * The color cyan.  In the default sRGB space.
     */
    public final static Color cyan = new Color(0, 255, 255);

    /**
     * The color cyan.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color CYAN = cyan;

    /**
     * The color blue.  In the default sRGB space.
     */
    public final static Color blue = new Color(0, 0, 255);

    /**
     * The color blue.  In the default sRGB space.
     *
     * @since 1.4
     */
    public final static Color BLUE = blue;


    private int mR;
    private int mG;
    private int mB;
    private float mA;

    public Color(int red, int green, int blue, float alpha) {
        this.mR = red;
        this.mG = green;
        this.mB = blue;
        this.mA = alpha;
    }

    public Color(int red, int green, int blue) {
        this(red, green, blue, 1.0f);
    }

    public int getRed() {
        return mR;
    }

    public void setRed(int red) {
        this.mR = red;
    }

    public int getGreen() {
        return mG;
    }

    public void setGreen(int green) {
        this.mG = green;
    }

    public int getBlue() {
        return mB;
    }

    public void setBlue(int blue) {
        this.mB = blue;
    }

    public float getAlpha() {
        return mA;
    }

    public void setAlpha(float alpha) {
        this.mA = alpha;
    }

    @Override
    public String toString() {
        return "Color{" +
                "red=" + mR +
                ", green=" + mG +
                ", blue=" + mB +
                ", alpha=" + mA +
                '}';
    }

    public static Color fromRGBA(int red, int green, int blue, float alpha) {
        return new Color(red, green, blue, alpha);
    }

    public static Color fromHex(String hex) {
        hex = hex.replace("#", "");
        int red = Integer.parseInt(hex.substring(0, 2), 16);
        int green = Integer.parseInt(hex.substring(2, 4), 16);
        int blue = Integer.parseInt(hex.substring(4, 6), 16);
        float alpha = hex.length() == 8 ? Integer.parseInt(hex.substring(6, 8), 16) / 255.0f : 1.0f;
        return new Color(red, green, blue, alpha);
    }


    public String toHex() {
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append(Integer.toHexString(mR));
        sb.append(Integer.toHexString(mG));
        sb.append(Integer.toHexString(mB));
        if (mA < 1.0f) {
            sb.append(String.format("%02X", (int) (mA * 255)));
        }
        return sb.toString();
    }


    /**
     * rgba 或 rgb 格式颜色转Color对象
     *
     * @param rgbaString
     * @return
     */
    public static Color fromRGBAString(String rgbaString) throws IllegalArgumentException {
        Pattern pattern = Pattern.compile("(rgba|rgb)\\((\\d+),\\s*(\\d+),\\s*(\\d+)(?:,\\s*([\\d.]+))?\\)");
        Matcher matcher = pattern.matcher(rgbaString);
        if (matcher.matches()) {
            int red = Integer.parseInt(matcher.group(2));
            int green = Integer.parseInt(matcher.group(3));
            int blue = Integer.parseInt(matcher.group(4));
            float alpha = matcher.group(5) != null ? Float.parseFloat(matcher.group(5)) : 1.0f;
            return new Color(red, green, blue, alpha);
        } else {
            throw new IllegalArgumentException("Invalid RGBA color string: " + rgbaString);
        }
    }

    public String toRGBA() {
        StringBuilder sb = new StringBuilder();
        sb.append("rgba(");
        sb.append(mR);
        sb.append(",");
        sb.append(mG);
        sb.append(",");
        sb.append(mB);
        sb.append(",");
        sb.append(mA);
        sb.append(")");
        return sb.toString();
    }


    /**
     * 字符串颜色 支持rgba() rgb  16进制颜色 转为Color对象
     *
     * @param colorStr
     * @return
     */
    public static Color colorStringToHex(String colorStr) throws IllegalArgumentException {
        if (colorStr.contains("rgb") || colorStr.contains("rgba")) {
            return fromRGBAString(colorStr);
        } else {
            return fromHex(colorStr);
        }
    }
}
